<?php

namespace App\Http\Controllers\ShopCar;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ShopCarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        $shopCar = session('shopCar');
        $shopCar_arr = ($shopCar != null ? explode(',',$shopCar) : array());
         foreach($shopCar_arr as $value) {
            // 获取分号的位置
            $index = strpos($value,':');
            // 遍历商品id,储存成一个数组
            $goods_id[] = substr($value,0,$index);
            // 遍历商品配置id,储存成一个数组
            $config_id[] = substr($value,$index+3);
            // 遍历商品数量,储存成一个数组
            $goods_count[] = substr($value,$index+1,1);    
        }
        // dd($config_id);
        // dd($config_id);
        // 在数据库中进行查询数据
        $goods_car = DB::table('goods_config')
        -> join('goods','goods.id','=','goods_config.goods_id')
        -> join('goods_config_list','goods_config_list.id','=','goods_config.goods_config_id')
        -> join('goods_datalist_pic','goods_datalist_pic.id','=','goods_config.goods_datalist_pic')
        -> whereIn('goods_config.id',$config_id)
        -> select('goods_datalist_pic.goods_datalist_pic1','goods_config.id','goods_config.goods_id','goods_model','goods.goods_sort','goods_art','goods_stick','goods.goods_title','goods_boutique','goods_status','goods_price','goods_config_list.goods_config_cpu_brand','goods_config_list.goods_config_cpu_core','goods_config_list.goods_config_cpu_clock','goods_config_list.goods_config_cpu_type','goods_config_list.goods_config_cpu_ddr','goods_config_list.goods_config_cpu_series','goods_config_list.goods_config_disk_brand','goods_config_list.goods_config_disk_kind','goods_config_list.goods_config_disk_size','goods_config_list.goods_config_gpu_brand','goods_config_list.goods_config_gpu_type','goods_config_list.goods_config_gpu_ddr','goods_config_list.goods_config_gpu_size','goods_config_list.goods_config_mainborad_brand','goods_config_list.goods_config_mainborad_type','goods_config_list.goods_config_ram_brand','goods_config_list.goods_config_ram_ddr','goods_config_list.goods_config_ram_size')
        // -> whereIn('goods.id',$goods_id)
        -> get();
        foreach($goods_car as $key=>$value) {
            foreach($shopCar_arr as $k=>$v) {
                // 如果商品id等于session中的商品id 那么就在原数组中插入一个商品数量
                if(substr($v,0,$index) == $value['goods_id']) {
                    // 插入商品数量
                    $goods_car[$key]['count'] = substr($v,$index+1,1);
                    // 插入购物车单个物品不同数量的踪总计
                    $goods_car[$key]['count_price'] = (substr($v,$index+1,1)) * ($value['goods_price']);
                    // 插入原价
                    $goods_car[$key]['old_price'] = round($value['goods_price'] / 0.9,2);
                    break;
                }
            }
            
        }
<<<<<<< HEAD
        // dd($goods_car);
=======
        dd($goods_car);
>>>>>>> b1990139fd74a207cbbfdc7c165186e9f38d0e5d
        return view('home/ShopCar/index',['goods'=>$goods_car]);
    }

    /**
     * getShopcar()   ajax添加购物车方法,在session中储存1:1:1格式的字符,分别代表商品id,数量,配置id
     *
     * 
     * @return        如果添加成功,ajax返回添加购物车成功
     */
    public function getShopcar(Request $request)
    {
       // 商品id
       $goods_id = $request -> only('goods_id')['goods_id'];
       // 商品配置id
       $config_id = $request -> only('config_id')['config_id'];
       // 商品数量
       $goods_count = $request -> only('goods_count')['goods_count'];
       // 从session中读取购物车信息
       $shopCar = session('shopCar');
       // 判断session中的shopCar是否有值,如果有,则用逗号进行分割成一个一维数组,如果没有则赋值给一个空数组
       $shopCar_arr = ($shopCar != null ? explode(',',$shopCar) : array()); 
       // 定义商品数量标识符
       $count = 0;
        foreach($shopCar_arr as &$value) {
            // 获取分号的位置
            $index = strpos($value,':');
            // 遍历获取数组中的商品id,如果有id等于刚传入进来的商品id那么,就进行数量增加
            if(substr($value,0,$index) == $goods_id && substr($value,$index+3) == $config_id) {
                // 进行数量相加,并转换成整型
                $count = ((int) substr($value,$index+1,1) + $goods_count);
                $value = $goods_id.':'.$count.':'.$config_id;
                break;
            }
        }

        if($count == 0) {
            array_push($shopCar_arr,$goods_id.':'.$goods_count.':'.$config_id);
        }
        $request -> session() -> put('shopCar',implode(',',$shopCar_arr));
       echo "加入购物车成功";
       

    }


    function getTest() {
        $shopCar = session('shopCar');
        dd($shopCar);
<<<<<<< HEAD
        
=======
>>>>>>> b1990139fd74a207cbbfdc7c165186e9f38d0e5d
    }

    /**
     * getAdd()    用户退出登陆的同时,将购物车数据写入数据库
     * 
     */
    public function getAdd()
    {
        $shopCar = session('shopCar');
        $res = DB::table('goods_shopcar') -> insert(['shop_car'=>$shopCar,'user_id'=>1]);
        if($res) {
            echo 'aaa';
        }

    }

    /**
     * getRead()    用户登陆后从数据库中拿出数据
     * @return $data arr 返回购物车信息数组
     */
    public function getRead()
    {
        $user_id = 1;
        $data = DB::table('goods_shopcar') -> where('user_id','=',$user_id) -> get();
        dd($data);
    }

<<<<<<< HEAD
    public function getDeletecar(Request $request)
    {
        $id = $request -> only('id')['id'];
        // 从session中读取购物车信息
        $shopCar = session('shopCar');
        // 判断session中的shopCar是否有值,如果有,则用逗号进行分割成一个一维数组,如果没有则赋值给一个空数组
        $shopCar_arr = ($shopCar != null ? explode(',',$shopCar) : array()); 
        foreach($shopCar_arr as $key=>&$value) {
            // 获取分号的位置
            $index = strpos($value,':');
            // 遍历获取数组中的商品配置id,
            if(substr($value,$index+3) == $id) {
                unset($shopCar_arr[$key]);
                break;
            }
        }
        $request -> session() -> put('shopCar',implode(',',$shopCar_arr));
        echo json_encode($shopCar);
    }


=======
>>>>>>> b1990139fd74a207cbbfdc7c165186e9f38d0e5d

    
}
